package org.gten.gegedexin.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.gten.gegedexin.Data.QueryInformation;
import org.gten.gegedexin.IMAP.GetMail;
import org.gten.gegedexin.service.ReadAppendix;
import org.gten.gegedexin.service.ReadMail;
import org.gten.gegedexin.service.ReadMailList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class ReceiveEmail {
    @Autowired
    ReadMailList readMailList;
    @Autowired
    ReadMail readMail;
    @Autowired
    ReadAppendix readAppendix;
    @Autowired
    GetMail getMail;

    @RequestMapping("/getMailList")
    @CrossOrigin
    public JSONArray getMailList(String receiverID, QueryInformation queryInformation) throws IOException {
        System.out.println("321saf");
        getMail.storageMail();
        readMailList.setUser(receiverID);
        readMailList.readList(receiverID);
        return readMailList.readPage(queryInformation.getPage());
    }

    //  当用户点进一个邮件时，返回用户的邮件正文和附件列表
    @RequestMapping("/receiveMail")
    @ResponseBody
    @CrossOrigin
    public JSONObject receiveMail(String mailId){
        return readMail.readmail(mailId);
    }

    //  当用户选择下载一个附件时，返回一个附件
    @RequestMapping("/receiveAppendix")
    @CrossOrigin
    public void receiveAppendix(@RequestParam("filename") String filename,@RequestParam("mailID")String mailID, HttpServletResponse response){
        readAppendix.download(filename,mailID,response);
    }


//    //  用户刷新页面时，用IMAP更新文件列表
//    @RequestMapping("/updateMail")
//    @CrossOrigin
//    public void updateMail() throws IOException {
//        getMail.storageMail();
//    }

//    //  用户进入收件箱界面时，用IMAP获取邮件
//    //  获取用户的邮件列表，返回邮件列表总页数
//    @RequestMapping("/getMailList")
//    @CrossOrigin
//    public int getMailList(@RequestParam String receiverID) throws IOException {
//        getMail.storageMail();
//        readMailList.readList(receiverID);
//        return readMailList.returnPage();
//    }
//
//    //  返回邮件列表的一页
//    @RequestMapping("/getPage")
//    @ResponseBody
//    @CrossOrigin
//    public JSONArray getPage(@RequestParam int page){
//        return readMailList.readPage(page);
//    }
}
